home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-11-05 | 3.9 KB | 160 lines | [TEXT/CWIE] |
- import java.awt.*;
- import java.lang.*;
- import java.util.*;
-
- abstract class Device {
- Schematic schematic;
- Rectangle boundingBox;
- boolean highlighted;
-
- Device(Schematic s, Point p) {
- schematic = s;
- highlighted= false;
- }
-
- abstract public void draw(Graphics g);
-
- public boolean pick(Point p) {
- return boundingBox.inside(p.x, p.y);
- }
-
- public boolean isHighlighted() {
- return highlighted;
- }
-
- public void setHighlight(boolean b) {
- highlighted = b;
- }
-
- public void setPosition(Point p) {
- boundingBox.move(p.x, p.y);
- }
-
- public Point getPosition() {
- return new Point(boundingBox.x, boundingBox.y);
- }
-
- public Rectangle getBoundingBox() {
- return boundingBox;
- }
- }
-
- class Resistor extends Device {
-
- Resistor(Schematic s, Point p) {
- super(s, p);
- boundingBox = new Rectangle(p.x, p.y, 25, 100);
- }
-
- public void draw(Graphics g) {
- g.setColor(Color.black);
-
- int x = boundingBox.x;
- int y = boundingBox.y;
- int width = boundingBox.width;
- int height = boundingBox.height;
- int halfWidth = boundingBox.width/2;
- int quarterHeight = boundingBox.height/4;
-
- g.drawLine(x+halfWidth, y, x+halfWidth, y+quarterHeight);
- if (highlighted)
- g.fillRect(x, y+quarterHeight, width, quarterHeight*2);
- else
- g.drawRect(x, y+quarterHeight, width, quarterHeight*2);
- g.drawLine(x+halfWidth, y+quarterHeight*3,
- x+halfWidth, y+height);
- }
- }
-
- class Schematic extends Canvas {
- Vector devices;
- Device pickedDevice;
- Point offset;
-
- Schematic() {
- devices = new Vector();
- pickedDevice = null;
- }
-
- void addDevice(Device d) {
- devices.addElement(d);
- }
-
- void removeDevice(Device d) {
- devices.removeElement(d);
- }
-
- public void paint(Graphics g) {
- for (Enumeration e = devices.elements();
- e.hasMoreElements(); ) {
- Device d = (Device) e.nextElement();
- d.draw(g);
- }
- }
-
- public Device pickDevice(Point p) {
- for (Enumeration e = devices.elements();
- e.hasMoreElements(); ) {
- Device d = (Device) e.nextElement();
- if (d.pick(p)) {
- d.setHighlight(!d.isHighlighted());
- repaint();
- return d;
- }
- }
- return null;
- }
-
- public boolean mouseDown(Event e, int x, int y) {
- pickedDevice = pickDevice(new Point(x, y));
- Point p = pickedDevice.getPosition();
- offset = new Point(p.x-x, p.y-y);
- return true;
- }
-
- public boolean mouseDrag(Event e, int x, int y) {
- if (pickedDevice != null) {
- pickedDevice.setPosition( new Point(x+offset.x,
- y+offset.y) );
- repaint();
- return true;
- }
- return false;
- }
-
- public boolean mouseUp(Event e, int x, int y) {
- if (pickedDevice != null) {
- pickedDevice.setPosition( new Point(x+offset.x,
- y+offset.y) );
- pickedDevice.setHighlight(false);
- repaint();
- pickedDevice = null;
- return true;
- }
- return false;
- }
- }
-
- public class SchematicTest extends Frame {
- Schematic s;
-
- public SchematicTest() {
-
- super("Sample Electronics Schematic");
- setLayout( new BorderLayout());
- add("Center", s = new Schematic());
-
- s.addDevice( new Resistor(s, new Point(20, 100)));
- s.addDevice( new Resistor(s, new Point(130, 100)));
- s.addDevice( new Resistor(s, new Point(250, 100)));
-
- resize(300, 300);
- show();
- }
-
- public static void main(String args[]) {
- Frame f = new SchematicTest();
- }
- }
-
-